package com.zim.analysis.saika;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.zim.analysis.po.GpsPO;
import com.zim.analysis.util.FormatUtils;

/**
 * 整车数据处理
 * 
 * @author zi-m.cn
 *
 */
public class Gps0004 extends BaseHandle {

    public Gps0004(byte[] bytes) throws Exception {
        super(bytes);
    }

    public GpsPO analysis() throws Exception {

        GpsPO gpsInfo = new GpsPO();
        gpsInfo.setMessage(stringMsg);
        gpsInfo.setLength(validate());
        gpsInfo.setDeviceName(stringMsg.substring(2, 22));
        gpsInfo.setSerialNumber(message[11]);
        gpsInfo.setTime(getTime());

        gpsInfo.setObdStatus(FormatUtils.getBinIndex(message[23], 0));
        gpsInfo.setRunLight(FormatUtils.getBinIndex(message[23], 1));
        gpsInfo.setReturnCar(FormatUtils.getBinIndex(message[23], 2));
        gpsInfo.setReady(FormatUtils.getBinIndex(message[23], 3));
        gpsInfo.setLongRenting(FormatUtils.getBinIndex(message[23], 4));
        gpsInfo.setCtrlLock(FormatUtils.getBinIndex(message[23], 5));

        gpsInfo.setDoor(FormatUtils.getBinIndex(message[24], 0));
        gpsInfo.setPositioning(FormatUtils.getBinIndex(message[24], 1));
        gpsInfo.setCenterLock(FormatUtils.getBinIndex(message[24], 4));
        gpsInfo.setAlarm(FormatUtils.getBinIndex(message[24], 5));
        gpsInfo.setAccStatus(FormatUtils.getBinIndex(message[24], 6));
        gpsInfo.setChargeStatus(FormatUtils.getBinIndex(message[24], 7));

        gpsInfo.setSatellite(message[25] & 0xFF);
        double longitude = Double.parseDouble(stringMsg.substring(52, 55))
                + Double.parseDouble("0." + stringMsg.substring(55, 62)) / 60 * 100;
        gpsInfo.setLongitude(longitude);
        double latitude = Double.parseDouble(stringMsg.substring(62, 64))
                + Double.parseDouble("0." + stringMsg.substring(64, 70)) / 60 * 100;
        gpsInfo.setLatitude(latitude);

        gpsInfo.setDiretion((message[35] & 0xFF) << 1);
        gpsInfo.setGpsSpeed(message[36] & 0xFF);
        gpsInfo.setSpeed(message[37] & 0xFF);

        switch (message[38]) {
        case 0x0B:
            gpsInfo.setGear("E");
            break;
        case 0x0C:
            gpsInfo.setGear("R");
            break;
        case 0x0D:
            gpsInfo.setGear("N");
            break;
        case 0x0E:
            gpsInfo.setGear("D");
            break;
        case 0x0F:
            gpsInfo.setGear("P");
            break;
        default:
            gpsInfo.setGear("null");
            break;
        }

        gpsInfo.setSoc(((message[39] & 0xFF) << 8) + (message[40] & 0xFF));
        gpsInfo.setEndurance(((message[41] & 0xFF) << 8) + (message[42] & 0xFF));
        gpsInfo.setBatteryVoltage(message[43] & 0xFF);
        gpsInfo.setMileage(((message[44] & 0xFF) << 24) + ((message[45] & 0xFF) << 16) + ((message[46] & 0xFF) << 8)
                + (message[47] & 0xFF));
        gpsInfo.setTotalVolatge(((message[48] & 0xFF) << 8) + (message[49] & 0xFF));
        gpsInfo.setTotalElecticity(((message[50] & 0xFF) << 8) + (message[51] & 0xFF));

        int v1 = message[52] & 0xFF;
        int v2 = message[53] & 0xF0;
        int v3 = message[53] & 0x0F;
        gpsInfo.setVersion(v1 + "." + v2 + "." + v3);

        gpsInfo.setGprsSignal(message[54] & 0xFF);
        return gpsInfo;
    }

    protected Long getTime() throws Exception {
        String timeString = "20" + stringMsg.substring(32, 44);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = sdf.parse(timeString.toString());
        long time = date.getTime();
        if (time < 1511008433000L) {
            time = System.currentTimeMillis();
        }
        return time;
    }

}
